#!/usr/bin/perl
# Version 0.2.1

$testcase = "negotiation";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3 );

$success = 1;
open(DUMPFILE, $dumpfile) or $success = 0;

if($success eq 1) {
	
	$rt1=4711;
	$rt2=4711;
	
	while(<DUMPFILE>) {
		# only look at dd packets
		if(/OSPFv2-dd/) {
			if(/0\.0\.0\.1 >/) {
				
				# check if 0.0.0.1 is master
				if(/MS/) {					
					$rt1 = 1;
				} else {
					$rt1 = 0;
				}
				
			} elsif(/0\.0\.0\.2 >/) {
				# check if 0..0.2 is master

				if(/MS/) {
					$rt2 = 1;
				} else {
					$rt2 = 0;
				}
			}
			
		}
	}
	
	close(DUMPFILE);
}

# 0 = slave, 1 = master
if($rt1 eq 0) {
	$local_verdict[0] = 1;
} elsif ($rt1 eq 1 ) {
	$local_verdict[0] = 2;
} else {
	$local_verdict[0] = 3;
}

if($rt2 eq 0) {
	$local_verdict[1] = 2;
} elsif ($rt2 eq 1 ) {
	$local_verdict[1] = 1;
} else {
	$local_verdict[1] = 3;
}


# finally evaluate results and generate report
if ($local_verdict[0] eq 1) {
	print("The router with the lower ID becomes slave. Test PASSED.\n");
} elsif ($local_verdict[0] eq 2) {
	print("The router with thelower ID doesn't become slave. Test FAILED.\n");
} else {
	print("Test INCONCLUSIVE.\n");
}

if ($local_verdict[1] eq 1) {
	print("The router with the higher ID becomes master. Test PASSED.\n");
} elsif ($local_verdict[1] eq 2) {
	print("The router with the higher ID doesn't become master. Test FAILED.\n");
} else {
	print("Test INCONCLUSIVE.\n");
}

$verdict = $local_verdict[0];
if ($local_verdict[1] ne $verdict) {
	if($local_verdict[1] eq 2) {
		$verdict = 2;
	} elsif ($local_verdict[1] eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");